#include "mainwindow.h"
#include "QMediaPlayer"
#include <QMediaPlaylist>
#include <QDebug>
#include <QTime>
#include <QDir>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setFixedSize(800, 480);
    is_show = 0;
    index = 0;
    setWindowTitle("海贼迷");
    QString p = QDir::currentPath();
    p += "/1.mp3";
    QMediaPlayer  *music = new QMediaPlayer(this);//初始化音乐
    QMediaPlaylist *playlist = new QMediaPlaylist(this);//初始化播放列表
    playlist->setPlaybackMode(QMediaPlaylist::Loop);//设置播放模式(顺序播放，单曲循环，随机播放等)
    playlist->addMedia(QUrl::fromLocalFile(p));//添加歌曲，这里添加的是歌曲的路径
    music->setPlaylist(playlist);  //设置播放列表
    music->play();//播放歌曲

    label = new QLabel(this);
    label->setGeometry(0, 0, 600, 480);
    label->setPixmap(QPixmap(":/res/0.jpg").scaled(600, 480));

    auto_btn = new QPushButton(this);
    auto_btn->setGeometry(620, 0, 160, 80);
    auto_btn->setText("自动播放");
    auto_btn->setEnabled(false);
    connect(auto_btn, &QPushButton::clicked, this, &MainWindow::autoBtn_click);

    previous_btn = new QPushButton(this);
    previous_btn->setGeometry(620, 100, 160, 80);
    previous_btn->setText("上一张");
    previous_btn->setEnabled(false);
    connect(previous_btn, &QPushButton::clicked, this, &MainWindow::previousBtn_click);

    next_btn = new QPushButton(this);
    next_btn->setGeometry(620, 200, 160, 80);
    next_btn->setText("下一张");
    next_btn->setEnabled(false);
    connect(next_btn, &QPushButton::clicked, this, &MainWindow::nextBtn_click);

    pause_btn = new QPushButton(this);
    pause_btn->setGeometry(620, 300, 160, 80);
    pause_btn->setText("暂停");
    pause_btn->setEnabled(false);
    connect(pause_btn, &QPushButton::clicked, this, &MainWindow::pauseBtn_click);

    QVector<int > num;
    bool flag = false;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i=0; i<15; i++)
    {
        int ret = qrand()%16+1;
        for(int j=0; j<num.size(); j++)
        {
            if(num.at(j) == ret)
            {
                flag = true;
                break;
            }
        }
        if(flag)
        {
            flag = false;
            i--;
        }
        else
        {
            num.append(ret);
        }
    }
    flag = false;
    for(int i=1; i<17; i++)
    {
        for(int j=0; j<num.size(); j++)
        {
            if(num.at(j) == i)
            {
                flag = true;
                break;
            }
        }
        if(flag)
        {
            flag = false;
        }
        else
        {
            num.append(i);
            break;
        }
    }

    for(int i=0; i<16; i++)
    {
        QString str = QString(":/res/%1.jpg").arg(num.at(i));
        path.append(str);
    }
    path.append(":/res/1.png");


    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
    timer->start(3000);
}

MainWindow::~MainWindow()
{

}

void MainWindow::paintEvent(QPaintEvent *)
{
    if(path.isEmpty())
        return ;
    if(is_show == 0)
    {
        return ;
    }
    else if(is_show == 1)
    {
        delete label;
        label = NULL;
        is_show = 2;
    }
    else
    {
       QPainter painter(this);

        painter.drawPixmap(0, 0, 600, 480, QPixmap(path.at(index)).scaled(800, 480));
    }
}


void MainWindow::timeout()
{
    is_show = 1;
    timer->stop();
    disconnect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
    connect(timer, &QTimer::timeout, this, [=]{
        update();
        index++;
        if(index>=17)
            index = 0;
    });
    timer->start(2000);
    auto_btn->setEnabled(true);
    pause_btn->setEnabled(true);
}

void MainWindow::autoBtn_click()
{
    if(auto_btn->text() == "自动播放")
    {
        auto_btn->setText("手动播放");
        timer->stop();
        previous_btn->setEnabled(true);
        next_btn->setEnabled(true);
        pause_btn->setEnabled(false);
    }
    else
    {
        auto_btn->setText("自动播放");
        timer->start(2000);
        previous_btn->setEnabled(false);
        next_btn->setEnabled(false);
        pause_btn->setEnabled(true);

    }
}

void MainWindow::previousBtn_click()
{
    if(index <= 0)
        index = 16;
    else
        index--;
    update();
}

void MainWindow::nextBtn_click()
{
    if(index >= 16)
        index = 0;
    else
        index++;
    update();
}


void MainWindow::pauseBtn_click()
{
    if(pause_btn->text() == "暂停")
    {
        pause_btn->setText("继续");
        timer->stop();
    }
    else
    {
        pause_btn->setText("继续");
        timer->start(2000);
    }
}

